π Python Programming β Pre-Assessment Knowledge Organiser
Spring 1 β Lesson 5: Consolidation
1οΈβ£ Inputs, Outputs & Variables
π Key Ideas
- Variables store data
- Input gets data from the user
- Output shows information to the user
- Inputs are always strings unless converted
π§ Key Syntax
name = input("Enter your name: ")
print(name)
π Casting (Changing Data Type)
num = int(input("Enter a number: "))
print(num * 2)
β οΈ Common Mistakes
- Forgetting to cast numbers with
int()
- Misspelling variable names
- Missing quotation marks in input prompts
2οΈβ£ Selection (IF / ELIF / ELSE)
π Key Ideas
- Selection allows decisions
- Conditions must be True or False
.lower() is used to ignore capital letters
π§ IF / ELSE
if answer.lower() == "warsaw":
print("Correct")
else:
print("Incorrect")
π§ IF / ELIF / ELSE
if colour.lower() == "red":
print("Correct")
elif colour.lower() == "blue":
print("Correct")
elif colour.lower() == "yellow":
print("Correct")
else:
print("Incorrect")
β οΈ Common Mistakes
- Using multiple
if statements instead of elif
- Forgetting
: at the end of conditions
- Incorrect indentation
3οΈβ£ While Loops
π Key Ideas
- While loops repeat while a condition is True
- Used when the number of repeats is unknown
- Can be condition-based or
while True
π§ Conditional While Loop
while password != "Secret123":
print("Incorrect password")
password = input("Try again: ")
π§ while True with break
while True:
guess = input("Enter a colour: ")
if guess.lower() == "red":
print("Correct")
break
else:
print("Incorrect")
β οΈ Common Mistakes
- Forgetting to update the variable inside the loop
- Missing
break, causing infinite loops
4οΈβ£ For Loops
π Key Ideas
- Used when the number of repeats is known
range(start, stop) stops before the final number
π§ For Loop Example
for i in range(1, 13):
print(f"{i} x 8 = {i * 8}")
β οΈ Common Mistakes
- Off-by-one errors in
range
- Using the wrong variable inside the loop
5οΈβ£ Lists (1D Lists)
π Key Ideas
- Lists store multiple values
- Indexes start at 0
π§ Example List
sports = ["Shotput", "Javelin", "Discus"]
π§ Looping Through a List
for i in range(0, len(sports)):
print(sports[i])
β οΈ Common Mistakes
- Going past the last index
- Forgetting square brackets
[ ]
6οΈβ£ 2D Lists (Lists of Lists)
π Key Ideas
- A 2D list stores records (rows of related data)
- Access using two indexes
π§ Accessing Data
landmarks[i][0] # Name
landmarks[i][1] # Location
π§ Looping Through a 2D List
for i in range(0, 8):
print(f"{landmarks[i][0]} is in {landmarks[i][1]}")
π Searching a 2D List
search = input("Enter medal type: ")
for i in range(0, 8):
if medals[i][1] == search.lower():
print(f"{medals[i][0]} won {medals[i][1]}")
β οΈ Common Mistakes
- Forgetting
.lower()
- Missing
: after if
- Incorrect indentation
π§ͺ Exam Tips
- Check indentation first if code doesnβt work
- Read the question carefully: IF vs IF / ELIF
- Inputs are strings
- Lists start at index 0
range() stops before the final number